<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Pay as LibraryPay;
use app\common\model\dynamic\Dynamic as DynamicDynamic;
use app\common\model\dynamic\Review;
use app\common\model\dynamic\review\Zan as ReviewZan;
use app\common\model\dynamic\Zan;

/**
 * 动态接口
 */
class Dynamic extends Api
{
    protected $noNeedLogin = ['lists', 'infos', 'review_list'];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @ApiTitle (发布动态)
     * @ApiMethod (POST)
     * @ApiParams (name="images", type="string", required=true, sample="", description="多图")
     * @ApiParams (name="title", type="string", required=true, sample="", description="标题")
     * @ApiParams (name="content", type="string", required=true, sample="", description="内容")
     * @ApiParams (name="address", type="string", required=true, sample="", description="地址")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function pub_dynamic()
    {
        $param = request()->param();
        extract($param);
        $param['user_id'] = $this->auth->id;
        $model = new DynamicDynamic($param);
        $res = $model->allowField(true)->save();
        if ($res) {
            $this->success('发布成功');
        }
        $this->error('发布失败');
    }
    /**
     * @Author LK
     * @ApiTitle (动态列表)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function lists()
    {
        $list = DynamicDynamic::with(['user'])->order('dynamic.id desc')->paginate();
        foreach ($list as $v) {
            $v->is_zan = false;
            if ($this->auth->isLogin()) {
                $v->is_zan = Zan::where('d_id', $v->id)->where('user_id', $this->auth->id)->count() ? true : false;
            }
        }
        $this->success('', $list);
    }
    /**
     * @Author LK
     * @ApiTitle (动态详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="id")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function infos()
    {
        $param = request()->param();
        extract($param);
        $data = DynamicDynamic::with(['user'])->where('dynamic.id', $id)->find();
        $data->is_zan = false;
        if ($this->auth->isLogin()) {
            $data->is_zan = Zan::where('d_id', $data->id)->where('user_id', $this->auth->id)->count() ? true : false;
        }
        $data->views += 1;
        $data->allowField(true)->save();
        $this->success('', $data);
    }
    /**
     * @Author LK
     * @ApiTitle (动态评论)
     * @ApiMethod (POST)
     * @ApiParams (name="d_id", type="integer", required=true, sample="", description="动态ID")
     * @ApiParams (name="content", type="string", required=true, sample="", description="动态内容")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function pub_review()
    {
        $param = request()->param();
        extract($param);
        $param['user_id'] = $this->auth->id;
        $model = new Review($param);
        $res = $model->allowField(true)->save();
        if ($res) {
            $this->success('发布成功');
        }
        $this->error('发布失败');
    }
    /**
     * @Author LK
     * @ApiTitle (评论列表)
     * @ApiMethod (GET)
     * @ApiParams (name="d_id", type="integer", required=true, sample="", description="动态ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function review_list()
    {
        $param = request()->param();
        extract($param);
        $list = Review::with(['user'])->where('d_id', $d_id)->order('id desc')->paginate();
        foreach ($list as $v) {
            $v->is_del = false;
            if ($this->auth->id == $v->user_id) {
                $v->is_del =  true;
            }
            $v->is_zan = false;
            $v->zan_count = ReviewZan::where('d_id', $v->id)->count();
            if ($this->auth->isLogin()) {
                $v->is_zan = ReviewZan::where('d_id', $v->id)->where('user_id', $this->auth->id)->count() ? true : false;
            }
        }
        $this->success('', $list);
    }
    /**
     * @Author LK
     * @ApiTitle (评论删除)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function review_list_del()
    {
        $param = request()->param();
        extract($param);
        $data = Review::get(['id' => $id, 'user_id' => $this->auth->id]);
        if ($data) {
            $data->delete();
            $this->success();
        }
        $this->error();
    }
    /**
     * @Author LK
     * @ApiTitle (我的动态)
     * @ApiMethod (get)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function my_dynamic()
    {
        $list = DynamicDynamic::with(['user'])->where('user_id', $this->auth->id)->order('dynamic.id desc')->paginate();
        foreach ($list as $v) {
            $v->is_zan = false;
            if ($this->auth->isLogin()) {
                $v->is_zan = Zan::where('d_id', $v->id)->where('user_id', $this->auth->id)->count() ? true : false;
            }
        }

        $this->success('', $list);
    }

    /**
     * @Author LK
     * @ApiTitle (点赞)
     * @ApiMethod (POST)
     * @ApiParams (name="d_id", type="integer", required=true, sample="", description="动态ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function zan()
    {
        $param = request()->param();
        extract($param);
        $where = ['d_id' => $d_id, 'user_id' => $this->auth->id];
        $data = Zan::get($where);
        if ($data) {
            $res = $data->delete();
        } else {
            $res = Zan::create($where);
        }
        if ($res) {
            $this->success();
        }
        $this->error();
    }
    /**
     * @Author LK
     * @ApiTitle (评论点赞)
     * @ApiMethod (POST)
     * @ApiParams (name="d_id", type="integer", required=true, sample="", description="动态ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function review_zan()
    {
        $param = request()->param();
        extract($param);
        $where = ['d_id' => $d_id, 'user_id' => $this->auth->id];
        $data = ReviewZan::get($where);
        if ($data) {
            $res = $data->delete();
        } else {
            $res = ReviewZan::create($where);
        }
        if ($res) {
            $this->success();
        }
        $this->error();
    }
}
